using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System;

namespace MyUtils
{
	public static class Null<T> where T : class
	{
		static T null_instance;

		[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
		public static T Get
		{
			get
			{
				if (null_instance == null)
				{
					throw new InvalidOperationException("Null Object is not defined");
				}
				return null_instance;
			}
		}

		[SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
		public static void DefineNullObject(T obj)
		{
			if (null_instance != null)
			{
				throw new InvalidOperationException("Null object for '{0}' already defined".Arg(typeof(T).Name));
			}
			if (obj == null)
			{
				throw new ArgumentException("Can not define null object with null reference");
			}
			if (obj.GetType().Attribute<NullObjectAttribute>() == null)
			{
				throw new ArgumentException("Argument must be marked as NullObject");
			}
			null_instance = obj;
		}
	}
}